<?php
namespace app\api\controller;

use app\api\controller\Base;
use app\goods\model\ImsEweiShopGoods;
use app\member\model\ImsEweiShopMember;
use app\member\model\ImsEweiShopMemberAddress;
use think\facade\Config;

class Address extends Base
{
    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 用户地址列表
     */
    public function index()
    {

        $token = $this->request->get('token');
        $addrId = $this->request->get('addrId');

        $member = $this->getMemberInfoByToken($token);
        if (empty($member)) {
            $this->echojson(1,'登陆过期，请重新登陆', $token);
        }

        $addWhere = array();
        $addWhere['openid'] = $member['openid'];
        $addWhere['deleted'] = 0;
        if ($addrId) {
            $addWhere['id'] = $addrId;
        }
        $fields = "id,uniacid,openid,realname as name, mobile as phone, province as provinceid,city as cityid, area as countryid,address as street, isdefault as is_default,lng,lat";
        $data = ImsEweiShopMemberAddress::mk()->where($addWhere)->field($fields)->select();
        $this->echojson(0,'获取用户地址成功', $data);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 地址添加
     */
    public function add()
    {

        $token = $this->request->post('token');
        $name = $this->request->post('name');
        $phone = $this->request->post('phone');
        $provinceid = $this->request->post('provinceid');
        $cityid = $this->request->post('cityid');
        $countryid = $this->request->post('countryid');
        $street = $this->request->post('street');
        $is_default = $this->request->post('is_default',1);
        $lat = $this->request->post('lat',0);
        $lng = $this->request->post('lng',0);

        $member = $this->getMemberInfoByToken($token);
        if (empty($member)) {
            $this->echojson(1,'登陆过期，请重新登陆', $token);
        }

        if ($is_default == 1) {
            ImsEweiShopMemberAddress::mk()->where(['openid'=>$member['openid']])->update([
                'isdefault' => 0
            ]);
        }

        $data = ImsEweiShopMemberAddress::mk()->create([
            'uniacid' => 2,
            'openid' => $member['openid'],
            'realname' => $name,
            'mobile' => $phone,
            'province' => $provinceid,
            'city' => $cityid,
            'area' => $countryid,
            'address' => $street,
            'isdefault' => $is_default,
            'lat' => $lat,
            'lng' => $lng,
        ]);

        $this->echojson(0,'地址添加成功', $data);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 地址修改
     */
    public function edit()
    {

        $token = $this->request->post('token');
        $id = $this->request->post('id');
        $name = $this->request->post('name');
        $phone = $this->request->post('phone');
        $provinceid = $this->request->post('provinceid');
        $cityid = $this->request->post('cityid');
        $countryid = $this->request->post('countryid');
        $street = $this->request->post('street');
        $is_default = $this->request->post('is_default',1);
        $lat = $this->request->post('lat',0);
        $lng = $this->request->post('lng',0);

        if (empty($id)) {
            $this->echojson(1,'参数错误', []);
        }

        $member = $this->getMemberInfoByToken($token);
        if (empty($member)) {
            $this->echojson(1,'登陆过期，请重新登陆', $token);
        }
        $fields = "id,uniacid,openid,realname as name, mobile as phone, province as provinceid,city as cityid, area as countryid,address as street, isdefault as is_default,lng,lat";
        $data = ImsEweiShopMemberAddress::mk()->field($fields)->where(['id'=>$id])->find();

        if ($is_default == 1) {
            ImsEweiShopMemberAddress::mk()->where(['openid'=>$member['openid']])->update([
                'isdefault' => 0
            ]);
        }
        $res = ImsEweiShopMemberAddress::mk()->update([
            'id'=> $data['id'],
            'realname' => $name,
            'mobile' => $phone,
            'province' => $provinceid,
            'city' => $cityid,
            'area' => $countryid,
            'address' => $street,
            'isdefault' => $is_default,
            'lat' => $lat,
            'lng' => $lng,
        ]);
        

        $this->echojson(0,'修改成功', $res);
    }

    public function  remove()
    {
        $token = $this->request->post('token');
        $id = $this->request->post('id');

        if (empty($id)) {
            $this->echojson(1,'参数错误', []);
        }

        $tokenService = new TokenService();
        $smid = $tokenService->verifyToken($token);
        $member = ImsEweiShopMember::mk()->where(['id'=>$smid])->find();
        if (empty($member)) {
            $this->echojson(1,'登陆过期，请重新登陆', [$smid]);
        }
        $data = ImsEweiShopMemberAddress::mk()->where(['id'=>$id])->find();

        $res = ImsEweiShopMemberAddress::mk()->update([
            'id'=> $data['id'],
            'deleted' =>1,
        ]);

        $this->echojson(0,'修改成功', $res);
    }
}